home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / IE7proSetup_2.3.exe / plugins / accuweather / plugin.js < prev   
Text File  |  2007-11-20  |  14KB  |  335 lines

  1. ∩╗┐// ==UserScript==
  2. // @name          Accu Weather
  3. // @namespace     http://iescripts.org
  4. // @description      Display the local weather in the statusbar, Copy from Windows vista accu weather gadgets
  5. // @statussize    180
  6. // ==/UserScript==
  7.  
  8. (function()
  9. {
  10.   var plugin = PRO_plugin(@name);
  11.   var currentWeather = ""; 
  12.   var currentIcon = "ie7pro://pluginpath/images/01.png";  
  13.   var currentTip = "";
  14.  
  15.   var WeatherUnits;
  16.   var CurrentLocation;
  17.   var CurrentConditions;
  18.   var CurrentImages;
  19.   var CurrentForecasts;
  20.   var CurrentWatches;
  21.   var CurrentLinks;
  22.   var ErrorString="";
  23.   var DataComplete=false;
  24.   var UseDate;
  25.   var mySplitDate;
  26.   var LocRadar;
  27.   var tempmin;
  28.  
  29.   var LANG_chs = new Array("Φ«╛τ╜«σ£░τé╣σÆîΘÇëΘí╣",
  30.             "τ╛Äσ¢╜σì½µÿƒΣ║æσ¢╛",
  31.             "µ£¼σ£░σî║σì½µÿƒΣ║æσ¢╛(µ£¬σ┐àσ¡ÿσ£¿)",
  32.             "σê╖µû░µò░µì«",
  33.             "Φ«┐Θù«AccuWeather.com",
  34.             "µƒÑτ£ïµ£¼σ£░σñ⌐µ░öΘóäµèÑ",
  35.             "σñ⌐µ░öΘóäµèÑ",
  36.             "Σ╕Ç","Σ║î","Σ╕ë","σ¢¢","Σ║ö","σà¡","µùÑ"," µ£ÇΘ½ÿµ░öµ╕⌐:"," µ£ÇΣ╜ĵ░öµ╕⌐:");
  37.   var LANG_eng = new Array("Set Location and Options",
  38.             "U.S. Radar Image",
  39.             "Your Regional Radar Image (If available)",
  40.             "Refresh Data",
  41.             "Go to AccuWeather.com",
  42.             "Weather Warnings in your area",
  43.             "Weather Forecast",
  44.             "Mon","Tue","Wed","Thu","Fri","Sat","Sun"," Hi:"," Lo:");    
  45.   var lng = PRO_getLang();
  46.   switch(lng){
  47.     case "eng":
  48.     case "chs":
  49.                 eval("var LANG = LANG_" + lng + ";");
  50.             eval("var CONF = \"findLocation." + lng + ".html\"");
  51.         break;
  52.     default:
  53.         var LANG = LANG_eng;
  54.         var CONF = "findLocation.eng.html";
  55.         break;
  56.   }
  57.  
  58.   plugin.registerContextMenu(LANG[0], menuSetLocation);
  59.   plugin.registerContextMenu("separator"); // add an separator for menu 
  60.   plugin.registerContextMenu(LANG[1], menuShowRadar);
  61.   plugin.registerContextMenu(LANG[2], menuShowLocRadar);
  62.   plugin.registerContextMenu(LANG[3], UpdateWeather);
  63.   plugin.registerContextMenu(LANG[4], menuShowAWPage);
  64.   plugin.registerContextMenu(LANG[5], menuShowWarnPage);
  65.   plugin.ontabcreate = handleTabCreate;
  66.   plugin.onpopupclose = handlePopupClose;
  67.  
  68.   var reqObj = new Object();
  69.   reqObj.request = function(callback, cookie, url, userAgent, lastModify,qtype) {
  70.       var req;
  71.       req = PRO_xmlhttpRequest();
  72.       req.onreadystatechange = function() {
  73.           if (req.readyState == 4) {// completed
  74.               if (req.status < 400) {// only if "OK"
  75.                   try {
  76.                       callback(req, cookie);
  77.                   } catch(err) {
  78.                   }
  79.               } else {
  80.                   // PRO_log("There was a problem loading data :\n" + req.status+ "/" + req.statusText);
  81.               }
  82.               setTimeout(function() {req.forceClean();req = null;}, 1000);
  83.           }
  84.       }
  85.       try {
  86.           req.open(qtype, url);
  87.           req.setRequestHeader( "User-Agent", userAgent);
  88.           if(lastModify.length > 0)
  89.               req.setRequestHeader( "If-Modified-Since", lastModify);
  90.           req.send(null);
  91.  
  92.       } catch(err){ 
  93.       }
  94.       return req;
  95.   }
  96.  
  97.   UpdateWeather();
  98. // updates every 30 minutes
  99.   setInterval(UpdateWeather, 1000*60*30 );
  100.  
  101.   function menuSetLocation(cookie,url) {
  102.       plugin.popupWindow("ie7pro://pluginpath/" + CONF,"","dialogWidth:500;dialogHeight:600;resizable:yes",cookie);
  103.   }
  104.  
  105.   function menuShowAWPage(cookie,url) {
  106.         PRO_openInTab(CurrentConditions.url,1);
  107.     }
  108.  
  109.   function menuShowWarnPage(cookie,url) {
  110.         PRO_openInTab(CurrentWatches.url,1);
  111.     }
  112.  
  113. // possible future addition
  114. //  function ShowWarnPopup(cookie,url) {
  115. //      plugin.popupWindow("ie7pro://pluginpath/warning.html","","dialogWidth:500;dialogHeight:600;resizable:no",cookie);
  116. //    }
  117.  
  118.   function menuShowRadar(cookie,url) {
  119.       plugin.popupWindow("http://sirocco.accuweather.com/nx_mosaic_400x300c/SIR/inmSIRus_.gif","","dialogWidth:452;dialogHeight:398;resizable:no",cookie);
  120. //      plugin.popupWindow("http://sirocco.accuweather.com/nx_mosaic_400x300c/SIR/inmSIRKY_.gif","","dialogWidth:500;dialogHeight:400;resizable:yes",cookie);
  121.   }
  122.  
  123.   function menuShowLocRadar(cookie,url) {
  124.       LocRadar=CurrentImages.radar;
  125.       LocRadar=LocRadar.replace(/108x81/, "400x300");
  126.       plugin.popupWindow(LocRadar,"","dialogWidth:452;dialogHeight:398;resizable:yes",cookie);
  127.   }
  128.  
  129.   function handleTabCreate(cookie) {
  130.       plugin.setStatusInfo(currentIcon, currentWeather, currentTip);
  131.   }
  132.  
  133.   function handlePopupClose(cookie, url, returnVal) {
  134.       UpdateWeather();
  135.   }
  136.  
  137.   function processWeatherData(req, cookie) {
  138.       parseData(req.responseXML.documentElement);
  139.   }
  140.  
  141.   function UpdateWeather() {
  142. // Use "tempmin" to put a number at the end of the url to thwart the 30 minute lifespan of the document
  143. // accuweather.com sets a pages "Expires" date in the http header for 30 minutes after serving page
  144. // the extraneous parameter(ie. &extra=01) at the end of the url is 
  145. // ignored by accuweather, but prevents this script from just accessing the page in cache
  146. // the extra parameter gives it a unique url, as far as the cache is concerned
  147. // allowing for a refresh every minute if desired, rather than 30 minutes.
  148.       tempmin=0;
  149.       var tempday = new Date();
  150.       tempmin=tempday.getMinutes();
  151. // end - except for...  "." + tempmin +  ...down a few lines
  152.       var mylocation=PRO_getValue("weather_Location","10001");
  153.       var mymetric=PRO_getValue("weather_Unit","metric");
  154.       var met = 1;
  155.       if(mymetric == "english")  met = 0;
  156.       var queryurl = "http://vwidget.accuweather.com/widget/vista1/weather_data_v2.asp?location=" + mylocation + "&metric=" + met + "&extra=" + tempmin;
  157.       reqObj.request(processWeatherData, "", queryurl, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; Alexa Toolbar)", "Thu, 26 Aug 2002 08:24:16 GMT","GET");    
  158.   }
  159.  
  160. function Images(xmlNode) {
  161.     this.radar=xmlNode.getElementsByTagName('radar').item(0).firstChild.data;
  162. }
  163.  
  164. function Units(xmlNode) {
  165.     this.tempUnit=xmlNode.getAttribute('temp');
  166.     this.speedUnit=xmlNode.getAttribute('speed');
  167.     this.distUnit=xmlNode.getAttribute('dist');
  168.     this.presUnit=xmlNode.getAttribute('pres');
  169.     this.precUnit=xmlNode.getAttribute('prec');
  170. }
  171.  
  172. function Watches(xmlNode) {
  173.     this.zone=xmlNode.getAttribute('zone');
  174.     this.county=xmlNode.getAttribute('county');
  175.     this.isactive=xmlNode.getAttribute('isactive');
  176.     if (xmlNode.getElementsByTagName('url').item(0).firstChild!=null) {
  177.         this.url=xmlNode.getElementsByTagName('url').item(0).firstChild.data;
  178.     } else {
  179.         this.url="";
  180.     }
  181. }
  182.  
  183. function Location(xmlNode) {
  184.     this.city=xmlNode.getAttribute('city');
  185.     this.state=xmlNode.getAttribute('state');
  186.     this.time=xmlNode.getAttribute('time');
  187. }
  188.  
  189. function Conditions(xmlNode) {
  190.     this.url=xmlNode.getElementsByTagName('url').item(0).firstChild.data;
  191.     tempNode=xmlNode.getElementsByTagName('wxc').item(0);
  192.     this.temperature=tempNode.getAttribute('temp');
  193.     this.realFeel=tempNode.getAttribute('rft');
  194.     this.humidity=tempNode.getAttribute('hum');
  195.     this.text=tempNode.getAttribute('text');
  196.     this.icon=tempNode.getAttribute('icon');
  197.     this.windGust=tempNode.getAttribute('wgus');
  198.     this.windSpeed=tempNode.getAttribute('wspd');
  199.     this.windDir=tempNode.getAttribute('wdir');
  200.     this.visibility=tempNode.getAttribute('vis');
  201.     this.precip=tempNode.getAttribute('prec');
  202.     this.uvindex=tempNode.getAttribute('uvi');
  203.     this.uvtext=tempNode.getAttribute('uvt');
  204.     this.radarUrl=xmlNode.getElementsByTagName('radurl').item(0).firstChild.data;
  205. }
  206.  
  207. function Links(xmlNode) {
  208.     this.promo1Link=xmlNode.getElementsByTagName('promo1').item(0).getAttribute('url');
  209.     this.promo1Name=xmlNode.getElementsByTagName('promo1').item(0).getAttribute('name');
  210.     this.homeLink=xmlNode.getElementsByTagName('home').item(0).getAttribute('url');    
  211. }
  212.  
  213. function Forecasts(xmlNode) {
  214.     tempNodes=xmlNode.getElementsByTagName('day');
  215.     tempArray=new Array(tempNodes.length);
  216.     for (count=0;count<tempNodes.length;count++) {
  217.         tempArray[count]=new ForecastDay(tempNodes[count]);
  218.     }
  219.     this.forecastArray=tempArray;
  220. }
  221.  
  222. function ForecastDay(xmlNode) {
  223.     this.dayNumber=xmlNode.getAttribute('number');
  224.     this.date=xmlNode.getAttribute('date');
  225.     this.weekDay=xmlNode.getAttribute('wday');
  226.     switch (this.weekDay) {
  227.         case "Monday" : this.shortWeekDay=LANG[7];break;
  228.         case "Tuesday" : this.shortWeekDay=LANG[8];break;
  229.         case "Wednesday" : this.shortWeekDay=LANG[9];break;
  230.         case "Thursday" : this.shortWeekDay=LANG[10];break;
  231.         case "Friday" : this.shortWeekDay=LANG[11];break;
  232.         case "Saturday" : this.shortWeekDay=LANG[12];break;
  233.         case "Sunday" : this.shortWeekDay=LANG[13];break;
  234.     }
  235.     this.url=xmlNode.getElementsByTagName('url').item(0).firstChild.data;
  236.     
  237.     //Daytime forecast data
  238.     this.daytimeTextShort=xmlNode.getElementsByTagName('./daytime/txtshort').item(0).firstChild.data;
  239.     this.daytimeTextLong=xmlNode.getElementsByTagName('./daytime/txtlong').item(0).firstChild.data;
  240.     this.daytimeIcon=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('icon');
  241.     this.daytimeHigh=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('htmp');
  242.     this.daytimeRealFeelHigh=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('rfh');
  243.     this.daytimeWindSpeed=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('wspd');
  244.     this.daytimeWindDirection=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('wdir');
  245.     this.daytimeWindGust=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('wgus');
  246.     this.daytimeMaxUV=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('mxuv');
  247.     this.daytimeRain=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('rain');
  248.     this.daytimeSnow=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('snow');
  249.     this.daytimePrecip=xmlNode.getElementsByTagName('./daytime/wxf').item(0).getAttribute('prec');
  250.     
  251.     //Nighttime forecast data
  252.     this.nighttimeTextShort=xmlNode.getElementsByTagName('./nighttime/txtshort').item(0).firstChild.data;
  253.     this.nighttimeTextLong=xmlNode.getElementsByTagName('./nighttime/txtlong').item(0).firstChild.data;
  254.     this.nighttimeIcon=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('icon');
  255.     this.nighttimeLow=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('ltmp');
  256.     this.nighttimeRealFeelLow=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('rfl');
  257.     this.nighttimeWindSpeed=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('wspd');
  258.     this.nighttimeWindDirection=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('wdir');
  259.     this.nighttimeWindGust=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('wgus');
  260.     this.nighttimeRain=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('rain');
  261.     this.nighttimeSnow=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('snow');
  262.     this.nighttimePrecip=xmlNode.getElementsByTagName('./nighttime/wxf').item(0).getAttribute('prec');
  263. }
  264.  
  265. function parseData(xmlData) {
  266.     try {
  267.         CurrentLocation=new Location(xmlData.getElementsByTagName("./local").item(0));
  268.         WeatherUnits=new Units(xmlData.getElementsByTagName("./units").item(0));
  269.         CurrentConditions=new Conditions(xmlData.getElementsByTagName("./currentconditions").item(0));
  270.         CurrentImages=new Images(xmlData.getElementsByTagName("./images").item(0));
  271.         CurrentForecasts=new Forecasts(xmlData.getElementsByTagName("./forecast").item(0));
  272.         CurrentWatches=new Watches(xmlData.getElementsByTagName("./watchwarnareas").item(0));
  273.         CurrentLinks=new Links(xmlData.getElementsByTagName("./links").item(0));
  274.         DataComplete=true;
  275.         var mymetric=PRO_getValue("weather_Unit","metric");
  276.         if(mymetric == "metric" ) 
  277. // degree symbol doesn't show in all browsers
  278. // if you prefer to lose the C or F, swap the rem slashes around on the lines below
  279. //            met = " ";
  280.             met = "C";
  281.         else
  282. //            met = " ";
  283.             met = "F";
  284.  
  285. // display city name in statusbar?
  286.           var displaycity=PRO_getValue("weather_city_display","showcity");
  287.         if(displaycity == "hidecity" ) {
  288.             currentWeather = "Now: " + CurrentConditions.text + ", " + CurrentConditions.temperature + met;
  289.         }else{ 
  290.             currentWeather = "Now: " + CurrentConditions.text + ", " + CurrentConditions.temperature + met + "(" + CurrentLocation.city + ")";
  291.         } 
  292.         currentIcon = "ie7pro://pluginpath/images/" + CurrentConditions.icon  + ".png";           
  293. // use proportional(default) or monospaced(Courier New) font?
  294.           var usefont=PRO_getValue("weather_font",0);
  295.         if(usefont==1) {
  296.             var tipfont='</font><font size="4" face="Courier New">';
  297.         }else{
  298.             var tipfont="";
  299.         }
  300.         currentTip = '<font size="4"><B>' + CurrentLocation.city + LANG[6] + ' </B> (' + CurrentLocation.time + ')<br>' + tipfont;
  301. // which date format is used?
  302.           var displaydate=PRO_getValue("weather_date_format","usdate");
  303.         for(i=0;i<5;i++) {
  304.             UseDate = CurrentForecasts.forecastArray[i].date;
  305.             if(displaydate == "shtusdate" ) {
  306.                 mySplitDate = UseDate.split("/");
  307.                 UseDate = mySplitDate[0] + "/" + mySplitDate[1];
  308.             }
  309.             if(displaydate == "intdate" ) {
  310.                 mySplitDate = UseDate.split("/");
  311.                 UseDate = mySplitDate[1] + "/" + mySplitDate[0] + "/" + mySplitDate[2];
  312.             }
  313.             if(displaydate == "shtintdate" ) {
  314.                 mySplitDate = UseDate.split("/");
  315.                 UseDate = mySplitDate[1] + "/" + mySplitDate[0];
  316.             }
  317.             currentTip += UseDate + " " + CurrentForecasts.forecastArray[i].shortWeekDay + LANG[14] + CurrentForecasts.forecastArray[i].daytimeHigh + met + LANG[15] + CurrentForecasts.forecastArray[i].nighttimeLow +met + " \- " + CurrentForecasts.forecastArray[i].daytimeTextShort + "<br>";
  318.         }     
  319.         currentTip += " </font>";
  320. // show flag icon in statusbar and warning message in tooltip if active warning in set location
  321.         if(CurrentWatches.isactive > 0){
  322.             currentTip += '<font size="4"><B>*** SEVERE WEATHER WARNING!! ***<br>Check AccuWeather.com Warnings Page!</B></font>';
  323.             currentIcon = 'ie7pro://pluginpath/images/flag.png';
  324. // potential future addition don't use now, it will hang your browser
  325. //            ShowWarnPopup();
  326.         }
  327.         plugin.setStatusInfo(currentIcon, currentWeather,currentTip)
  328.     } catch (e) {
  329.  
  330.     }
  331. }
  332.  
  333. }
  334. )();
  335.